//
//  TECacheViewController.m
//  TintExample
//
//  Created by Kevin Wu on 9/3/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TECacheViewController.h"


@implementation TECacheViewController


#pragma mark - Memory

- (id)initWithData:(id)data
{
    self = [super init];
    if ( self ) {
        
        _test = [TECacheTest sharedObject];
        
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_sizeLabel);
    TTRelease(_operationLabel);
    _test = nil;
    
    [super dealloc];
}


#pragma mark - UIViewController

- (void)loadView
{
    [super loadView];
    
    [self addButtonWithTitle:@"start" selector:@selector(start:)];
    [self addButtonWithTitle:@"stop" selector:@selector(stop:)];
    [self addButtonWithTitle:@"clean" selector:@selector(clean:)];
    [self addButtonWithTitle:@"show" selector:@selector(show:)];
    
    
    UILabel *label = nil;
    
    label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12.0];
    label.textColor = [UIColor blackColor];
    label.lineBreakMode = UILineBreakModeTailTruncation;
    label.numberOfLines = 1;
    label.textAlignment = UITextAlignmentCenter;
    label.adjustsFontSizeToFitWidth = NO;
    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];
    _sizeLabel = label;
    
    label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12.0];
    label.textColor = [UIColor blackColor];
    label.lineBreakMode = UILineBreakModeTailTruncation;
    label.numberOfLines = 1;
    label.textAlignment = UITextAlignmentCenter;
    label.adjustsFontSizeToFitWidth = NO;
    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];
    _operationLabel = label;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_sizeLabel setFrameIfNeeded:TTRectMake(0.0, 100.0, 320.0, 44.0)];
    [_operationLabel setFrameIfNeeded:TTRectMake(0.0, 150.0, 320.0, 44.0)];
}


#pragma mark - Public

- (void)start:(id)sender
{
    [_test startTest];
}

- (void)stop:(id)sender
{
    [_test stopTest];
}

- (void)clean:(id)sender
{
    [_test.diskCache cleanAllCache];
}

- (void)show:(id)sender
{
    float size = [_test.diskCache totalCacheSize]/1024.0/1024.0;
    int count = [_test.diskCache totalCount];
    
    NSString *sizeString = [[NSString alloc] initWithFormat:@"count: %d, size: %f", count, size];
    _sizeLabel.text = sizeString;
    [sizeString release];
    
    NSString *operationString = [[NSString alloc] initWithFormat:@"data: %d - %d = %d, string: %d - %d = %d", 
                                 _test.addedData, _test.removedData, _test.addedData - _test.removedData, 
                                 _test.addedString, _test.removedString, _test.addedString - _test.removedString];
    _operationLabel.text = operationString;
    [operationString release];
}

@end
